package com.trhui.mallbook.config;

import com.trhui.mallbook.common.utils.ValidationUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author linqy
 */
public class MallBookConfig {

    /**
     * 是否打印调用日志，默认打印
     */
    private boolean debug = true;

    private String version = "1.0.0";
    /**
     * MallBook 公钥，不配置则用默认公钥
     */
    private String mallBookPublicKey;

    private String merchantPrivateKey;
    /**
     * 环境
     * true 生产
     * false 调试环境
     */
    private boolean prodEnvironment = true;
    /**
     * 商户号
     */
    private String merchantNo;

    /**
     * MallBook 接口地址，不配置则用默认地址
     */
    private String apiBase;
    /**
     * 建立连接后从服务器读取到可用资源所用的时间
     * 单位：毫秒
     */
    private int timeout = 6000;

    /**
     * 是否打印文件上传接口的调用日志，默认打印
     */
    private boolean fileUploadDebug = true;

    public MallBookConfig() {
    }


    public String getMallBookPublicKey() {
        return mallBookPublicKey;
    }

    public void setMallBookPublicKey(String mallBookPublicKey) {
        this.mallBookPublicKey = mallBookPublicKey;
    }

    public boolean isDebug() {
        return debug;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }


    public String getMerchantPrivateKey() {
        return merchantPrivateKey;
    }

    public void setMerchantPrivateKey(String merchantPrivateKey) {
        this.merchantPrivateKey = merchantPrivateKey;
    }

    public boolean isProdEnvironment() {
        return prodEnvironment;
    }

    public void setProdEnvironment(boolean prodEnvironment) {
        this.prodEnvironment = prodEnvironment;
    }

    public String getApiBase() {
        return apiBase;
    }

    public void setApiBase(String apiBase) {
        this.apiBase = apiBase;
    }

    public String getMerchantNo() {
        return merchantNo;
    }

    public void setMerchantNo(String merchantNo) {
        this.merchantNo = merchantNo;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public boolean isFileUploadDebug() {
        return fileUploadDebug;
    }

    public void setFileUploadDebug(boolean fileUploadDebug) {
        this.fileUploadDebug = fileUploadDebug;
    }

    private static Map<String, MallBookConfig> configMap = new HashMap<>();


    public synchronized void init() {
        configMap.put(merchantNo, this);
    }

    public static MallBookConfig getConfig(String merchantNo) {
        ValidationUtil.checkNotNullAndThrow(merchantNo, "请求参数merchantNo必传");

        MallBookConfig config = configMap.get(merchantNo);

        ValidationUtil.checkNotNullAndThrow(config, "merchantNo[" + merchantNo + "]未初始化MallBook配置");

        ValidationUtil.checkNotNullAndThrow(config.getMerchantPrivateKey(), "请检查MallBook配置文件商户私钥");

        ValidationUtil.checkNotNullAndThrow(config.getMerchantNo(), "配置文件MallBook商户号必填");
        return config;
    }
}
